package org.angelica.modules.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.angelica.modules.account.dao.AccRolePermDao;
import org.angelica.modules.account.entity.AccRolePermDO;
import org.angelica.modules.account.service.AccRolePermService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;

/**
 * @author aizhimin
 */
@Service
public class AccRolePermServiceImpl extends ServiceImpl<AccRolePermDao, AccRolePermDO> implements AccRolePermService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRolePerms(Long roleId, String[] perms) {
        //删除角色与权限关系
        this.remove(new QueryWrapper<AccRolePermDO>().eq("role_id",roleId));

        //保存角色与权限关系
        List<AccRolePermDO> rolePermList = Lists.newArrayList();
        for(String perm:perms){
            rolePermList.add(AccRolePermDO.builder()
                    .roleId(roleId)
                    .permId(Long.valueOf(perm))
                    .gmtCreate(Calendar.getInstance().getTime())
                    .build());
        }
        this.saveBatch(rolePermList);
    }
}
